package HRMPROJECT.VIEWS;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import HRMPROJECT.CONTROLLERS.DepartmentController;
import HRMPROJECT.CONTROLLERS.EmployeesController;
import HRMPROJECT.MODELS.EmployeesEntity;
import HRMPROJECT.MODELS.SecurityContexts;

public class frmEmployees extends JFrame {

    private final JPanel contentPane;
    private final JTextField txtHoVaTen;
    private final JTextField txtDiaChi;
    private final JTextField txtDienThoai;
    private final JTextField txtBac;
    private final JTable jtbLoadData;
    private final JComboBox cboPhongBan;
    private final JCheckBox chkStatus;

    private Connection m_objConnection = null;
    private static Connection objConn;
    private static SecurityContexts objSec;
    private SecurityContexts m_objSecurity = null;
    private EmployeesController m_objEmpController = null;
    private DepartmentController m_objDepController = null;
    private EmployeesEntity objEntity = null;
    private DefaultTableModel m_objDtm = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frmEmployees frame = new frmEmployees(objConn, objSec);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frmEmployees(Connection objConnection, SecurityContexts objSecurity) {
        m_objConnection = objConnection;
        objConn = m_objConnection;
        m_objSecurity = objSecurity;
        objSec = m_objSecurity;
        m_objDtm = new DefaultTableModel();
        m_objEmpController = new EmployeesController(m_objConnection, m_objSecurity);
        m_objDepController = new DepartmentController(m_objConnection, m_objSecurity);

        setResizable(false);
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 593, 474);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "Qu\u1EA3n l\u00FD Th\u00F4ng Tin Nh\u00E2n Vi\u00EAn",
                TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
        panel.setBounds(0, 0, 577, 436);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("Họ và Tên :");
        lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel.setBounds(10, 26, 125, 22);
        panel.add(lblNewLabel);

        txtHoVaTen = new JTextField();
        txtHoVaTen.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtHoVaTen.setBounds(99, 25, 468, 24);
        panel.add(txtHoVaTen);
        txtHoVaTen.setColumns(10);

        chkStatus = new JCheckBox("Trạng thái");
        chkStatus.setFont(new Font("Cambria", Font.PLAIN, 14));
        chkStatus.setBounds(406, 126, 128, 26);
        panel.add(chkStatus);

        JButton btnDong = new JButton("Thêm Mới");
        btnDong.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 1. Kiểm tra thông tin các điều khiển trên frame sử dụng
                // Regular Expression (học viên thực hiện).
                // 2. Lấy thông tin từ trên giao diện xuống --> objects
                InsertUpdateOf(false);
            }
        });
        btnDong.setIcon(new ImageIcon(frmEmployees.class.getResource("/HRMPROJECT/IMAGES/edoc_register.png")));
        btnDong.setBackground(new Color(135, 206, 235));
        btnDong.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnDong.setBounds(447, 159, 120, 32);
        panel.add(btnDong);

        JLabel lblaCh = new JLabel("Địa Chỉ :");
        lblaCh.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblaCh.setBounds(10, 60, 125, 22);
        panel.add(lblaCh);

        txtDiaChi = new JTextField();
        txtDiaChi.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtDiaChi.setColumns(10);
        txtDiaChi.setBounds(99, 59, 468, 24);
        panel.add(txtDiaChi);

        JLabel lblSinThoi = new JLabel("Số điện thoại :");
        lblSinThoi.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblSinThoi.setBounds(10, 94, 125, 22);
        panel.add(lblSinThoi);

        txtDienThoai = new JTextField();
        txtDienThoai.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtDienThoai.setColumns(10);
        txtDienThoai.setBounds(99, 93, 468, 24);
        panel.add(txtDienThoai);

        JLabel lblBc = new JLabel("Bậc :");
        lblBc.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblBc.setBounds(10, 128, 125, 22);
        panel.add(lblBc);

        txtBac = new JTextField();
        txtBac.setFont(new Font("Cambria", Font.PLAIN, 14));
        txtBac.setColumns(10);
        txtBac.setBounds(99, 127, 301, 24);
        panel.add(txtBac);

        JButton btnLuu = new JButton("Cập Nhật");
        btnLuu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // 1. Kiểm tra thông tin các điều khiển trên frame sử dụng
                // Regular Expression (học viên thực hiện).
                // 2. Lấy thông tin từ trên giao diện xuống --> objects
                InsertUpdateOf(true);
            }
        });
        btnLuu.setIcon(new ImageIcon(frmEmployees.class.getResource("/HRMPROJECT/IMAGES/action_settings.png")));
        btnLuu.setBackground(new Color(135, 206, 235));
        btnLuu.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnLuu.setBounds(329, 159, 114, 32);
        panel.add(btnLuu);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 204, 557, 221);
        panel.add(scrollPane);

        jtbLoadData = new JTable();
        jtbLoadData.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                // 1. Lấy thông tin từ JTable
                objEntity = new EmployeesEntity();
                objEntity.setEmpId(Integer.valueOf("" + m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 0)));
                objEntity.setDepartId(Integer.valueOf("" + m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 1)));
                objEntity.setFullName(String.valueOf(m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 2)));
                objEntity.setAddress(String.valueOf(m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 3)));
                objEntity.setPhone(String.valueOf(m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 4)));
                objEntity.setRank(Integer.valueOf("" + m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 5)));

                if (String.valueOf(m_objDtm.getValueAt(jtbLoadData.getSelectedRow(), 6)).equals("Đang làm việc")) {
                    objEntity.setStatus(true);
                } else {
                    objEntity.setStatus(false);
                }

                // 2. Hiển thị thông tin lên điều khiển trên forms
                txtHoVaTen.setText(objEntity.getFullName());
                txtDiaChi.setText(objEntity.getAddress());
                txtDienThoai.setText(objEntity.getPhone());
                txtBac.setText("" + objEntity.getRank());
                if (objEntity.isStatus())
                    chkStatus.setSelected(true);
                else
                    chkStatus.setSelected(false);
            }
        });
        scrollPane.setViewportView(jtbLoadData);

        JLabel lblPhngBan = new JLabel("Phòng Ban:");
        lblPhngBan.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblPhngBan.setBounds(10, 164, 89, 27);
        panel.add(lblPhngBan);

        cboPhongBan = new JComboBox();
        cboPhongBan.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent arg0) {
                getDataByCombobox();
            }
        });
        cboPhongBan.setBounds(99, 159, 220, 32);
        panel.add(cboPhongBan);

        // Hiển thị phòng ban lên comboBox
        try {
            m_objDepController.getDepartmentOf(cboPhongBan);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    void getDataByCombobox() {
        try {
            int DepartId = m_objDepController.getSelectedValue(cboPhongBan, cboPhongBan.getSelectedIndex());
            m_objDtm = m_objEmpController.EmployeeByIdTableModels(DepartId);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        jtbLoadData.setModel(m_objDtm);
    }

    void InsertUpdateOf(boolean bFlag) {
        if (objEntity == null) {
            objEntity = new EmployeesEntity();
        }
        objEntity.setFullName(txtHoVaTen.getText());
        objEntity.setAddress(txtDiaChi.getText());
        objEntity.setPhone(txtDienThoai.getText());
        objEntity.setRank(Integer.parseInt("" + txtBac.getText()));
        objEntity.setStatus(chkStatus.isSelected());
        objEntity.setDepartId(m_objDepController.getSelectedValue(cboPhongBan, cboPhongBan.getSelectedIndex()));

        // 3. Ủy quyền thực thi xuống DB thông qua Controllers
        try {
            /* TH: Cập nhật thông tin Nhân Viên */
            if (bFlag) {
                if (m_objEmpController.UpdateEmpoyee(objEntity)) {
                    getDataByCombobox();
                }
            } else { /* TH: Thêm mới thông tin Nhân Viên */
                if (m_objEmpController.InsertEmployee(objEntity)) {
                    getDataByCombobox();
                }
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}
